﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using System.IO;
using System.Data;
using TaxBusiness.Model;
using TaxBusiness.BLL;



namespace WebSites.Controllers
{
    [AppActionAttributeFilter(Message = "controller")]
    public class MainController : Controller
    {
        //信达税务师事务所客户信息查询系统
        // GET: /Main/
        class MenuList
        {
            public int menuid { get; set; }
            public string icon { get; set; }
            public string menuname { get; set; }
            public List<MenuData> submenu { get; set; }
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult DXYW()
        {
            return View();
        }

        public ActionResult CreateBusiness()
        {
            return View();
        }

        public ActionResult UpdateBusiness()
        {
            return View();
        }

        public ActionResult UpdateCheckedBusiness()
        {
            return View();
        }

        public ActionResult CheckBusiness()
        {
            return View();
        }

        public ActionResult ChargingMoney()
        {
            return View();
        }

        public ActionResult ViewDetail()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
            {
                ViewData["BusinessID"] = Request.QueryString["id"].ToString();
            }
            return View();
        }

        public ActionResult CharingList()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
            {
                ViewData["BusinessID"] = Request.QueryString["id"].ToString();
            }
            return View();
        }

        public ActionResult SearchList()
        {
            return View();
        }

        public ActionResult CountBill()
        {
            return View();
        }

        public ActionResult CustomerList()
        {
            return View();
        }

        public ActionResult CustomerBusiness()
        {
            return View();
        }

        public ActionResult ReBill()
        {
            return View();
        }

        public ActionResult CountBusiness()
        {
            return View();
        }

        public ActionResult OverTimeList()
        {
            return View();
        }

        public ActionResult GetMenu()
        {
            MenuBLL menubll = new MenuBLL();
            List<MenuList> menulist = new List<MenuList>();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            int userid = int.Parse(Session["UserID"].ToString());
            List<MenuData> menudata = menubll.GetList(0, userid);
            foreach(MenuData menuitem in menudata)
            {
                MenuList menu = new MenuList();
                menu.menuid = menuitem.ID;
                menu.icon = menuitem.WebIcon;
                menu.menuname = menuitem.Name;
                menu.submenu = menubll.GetList(menuitem.ID, userid);
                menulist.Add(menu);
            }
            string json = serializer.Serialize(menulist);
            return Content(json);
        }

        public ActionResult GetList()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string order = Request.Form["order"].ToString();
            int pageIndex = int.Parse(Request.Form["page"].ToString());
            int pageSize = int.Parse(Request.Form["rows"].ToString());
            string keyWord = Request.Form["sort"].ToString();
            string whereString = " 1=1";
            int count;
            BusinessBLL businessbll = new BusinessBLL();
            List<Business> businesslist = new List<Business>();
            BusinessData listdata = new BusinessData();
            
            if (Session["AuthID"].ToString() == "1")
            {
                whereString += " AND FillInID = " + Session["UserID"].ToString();
                whereString += " And verifydate is null";
            }
            if (Session["AuthID"].ToString() == "2")
            {
                whereString += " AND (owingmoney<0 and paidup=0 or billdate is null or verifydate is null)";
            }
            businesslist = businessbll.GetList(pageSize, pageIndex, whereString, keyWord, order, out count);
            listdata.total = count;
            listdata.rows = businesslist;
            string json = serializer.Serialize(listdata);
            return Content(json);
        }

        public ActionResult GetListByType()
        {
            string type1 = Request.Form["type1"].ToString();
            string type2 = Request.Form["type2"].ToString();
            string order = Request.Form["order"].ToString();
            int pageIndex = int.Parse(Request.Form["page"].ToString());
            int pageSize = int.Parse(Request.Form["rows"].ToString());
            string keyWord = Request.Form["sort"].ToString();
            string customerName = "";
            string whereString = "1=1";
            int count;
            int businessType1,businessType2;

            try
            {
                customerName = Request.Form["customer"].ToString();
            }
            catch { }
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            BusinessBLL businessbll = new BusinessBLL();
            BusinessTypeBLL typebll = new BusinessTypeBLL();
            List<Business> businesslist = new List<Business>();
            BusinessData listdata = new BusinessData();

            if (type1 != "" && type1 != null)
                businessType1 = int.Parse(type1);
            else
                businessType1 = -2;
            
            if (type2 != "" && type2 != null)
                businessType2 = int.Parse(type2);
            else
                businessType2 = -2;

            if (businessType1>-1 && businessType2 == -1)
            {
                string[] typelist = typebll.GetTypeArray(businessType1);
                whereString += " AND BusinessType IN (" + string.Join(",", typelist) + ")";
            }
            
            if (businessType1 > -1 && businessType2 > -1)
            {
                whereString += " AND BusinessType = " + businessType2.ToString();
            }

            if (businessType1 > -1 && businessType2 < -1)
            {
                whereString += " AND BusinessType = " + businessType1.ToString();
            }

            if (customerName != "")
            {
                whereString += " AND TheName like '%" + customerName +"%'";
            }

            if (Session["AuthID"].ToString() == "1")
            {
                whereString += " AND FillInID = " + Session["UserID"].ToString();
            }
            if (Session["AuthID"].ToString() == "2")
            {
                if (Request.Form["showall"] == null || Request.Form["showall"].ToString() == "" || Request.Form["showall"].ToString() == "false")
                {
                    whereString += " AND (owingmoney<0 and paidup=0 or billdate is null or verifydate is null)";
                }
            }
            businesslist = businessbll.GetList(pageSize, pageIndex, whereString, keyWord, order, out count);
            listdata.total = count;
            listdata.rows = businesslist;
            string json = serializer.Serialize(listdata);
            return Content(json);
        }

        private string GetWhereString()
        {
            string whereString = "1=1";
            int businessType1, businessType2;
            BusinessTypeBLL typebll = new BusinessTypeBLL();

            if (Request["type1"] != null && Request["type1"].ToString() != "")
                businessType1 = int.Parse(Request["type1"].ToString());
            else
                businessType1 = -2;

            if (Request["type2"] != null && Request["type2"].ToString() != "")
                businessType2 = int.Parse(Request["type2"].ToString());
            else
                businessType2 = -2;

            if (businessType1 > -1 && businessType2 == -1)
            {
                string[] typelist = typebll.GetTypeArray(businessType1);
                whereString += " AND BusinessType IN (" + string.Join(",", typelist) + ")";
            }

            if (businessType1 > -1 && businessType2 > -1)
            {
                whereString += " AND BusinessType = " + businessType2.ToString();
            }

            if (businessType1 > -1 && businessType2 < -1)
            {
                whereString += " AND BusinessType = " + businessType1.ToString();
            }
            if (Session["AuthID"].ToString() == "2" || Session["AuthID"].ToString() == "4")
            {
                if (Request["theOffice"] != null && Request["theOffice"].ToString() != "" && Request["theOffice"].ToString() != "-1")
                {
                    whereString += " AND TheOffice = " + Request["theOffice"].ToString();
                }
            }
            else
            {
                whereString += " AND TheOffice = " + Session["OfficeID"].ToString();
            }

            if (Request["theDepartment"] != null && Request["theDepartment"].ToString() != "" && Request["theDepartment"].ToString() != "-1")
            {
                whereString += " AND TheDepartment = " + Request["theDepartment"].ToString();
            }
            if (Request["BillDepartment"] != null && Request["BillDepartment"].ToString() != "" && Request["BillDepartment"].ToString() != "-1" && Request["BillDepartment"].ToString() != "0")
            {
                whereString += " AND BillDepartment = " + Request["BillDepartment"].ToString();
            }
            if (Request["BillDepartment"] != null && Request["BillDepartment"].ToString() != "" && Request["BillDepartment"].ToString()== "0")
            {
                whereString += " AND BillDepartment is null";
            }
            if (Request["customerName"] != null && Request["customerName"].ToString() != "" && Request["customerName"].ToString() != "-1")
            {
                whereString += "AND TheName like '%" + Request["customerName"].ToString() + "%'";
            }
            if (Request["beginDate"] != null && Request["beginDate"].ToString() != "")
            {
                whereString += " AND ChargingDate >='" + Request["beginDate"].ToString() + "'";
            }

            if (Request["endDate"] != null && Request["endDate"].ToString() != "")
            {
                whereString += " AND ChargingDate <='" + Request["endDate"].ToString() + "'";
            }

            if (Request["isOverTime"] != null && Request["isOverTime"].ToString() != "" && Request["isOverTime"].ToString() == "true")
            {
                whereString += " AND DATEDIFF(month,GETDATE(),EndDate)<=1 AND CurrentState='0' AND verifyDate is not null";
            }

            if (Request["isOwingMoney"] != null && Request["isOwingMoney"].ToString() != "" && Request["isOwingMoney"].ToString() == "true")
            {
                whereString += " AND owingmoney<0 AND paidup=0 AND verifyDate is not null";
            }
            return whereString;
        }
        public ActionResult GetSearchList()
        {
            string order = Request.Form["order"].ToString();
            int pageIndex = int.Parse(Request.Form["page"].ToString());
            int pageSize = int.Parse(Request.Form["rows"].ToString());
            string keyWord = Request.Form["sort"].ToString();
              int count;

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            BusinessBLL businessbll = new BusinessBLL();
            List<Business> businesslist = new List<Business>();
            BusinessData listdata = new BusinessData();

            string whereString = GetWhereString();
            businesslist = businessbll.GetList(pageSize, pageIndex, whereString, keyWord, order, out count);
            listdata.total = count;
            listdata.rows = businesslist;
            string json = serializer.Serialize(listdata);
            return Content(json);
        }

        public ActionResult DataExportToExcel()
        {
            DataTable table = new DataTable();
            string order = "DESC";
            int pageIndex = 0;
            int pageSize = 10;
            string keyWord = "ID";
            int count;
            BusinessBLL businessbll = new BusinessBLL();
            //BusinessTypeBLL typebll = new BusinessTypeBLL();
            List<Business> businesslist = new List<Business>();
            string whereString = GetWhereString();
            businesslist = businessbll.GetList(pageSize, pageIndex, whereString, keyWord, order, out count);
            table = businessbll.ListToDataTable(businesslist);
            MemoryStream output = DataRenderToExcel.RenderDataTableToExcel(table) as MemoryStream;
            //Return the result to the end user
            return File(output.ToArray(),   //The binary data of the XLS file
             "application/vnd.ms-excel",//MIME type of Excel files
             "业务报表" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls");
        }

        public ActionResult GetOverTimeList()
        {
            string order = Request.Form["order"].ToString();
            int pageIndex = int.Parse(Request.Form["page"].ToString());
            int pageSize = int.Parse(Request.Form["rows"].ToString());
            string keyWord = Request.Form["sort"].ToString();
            int count;
            string sqlString = "1=1";
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            BusinessBLL businessbll = new BusinessBLL();
            List<Business> businesslist = new List<Business>();
            BusinessData listdata = new BusinessData();

            if (Request["customerName"] != null && Request["customerName"].ToString() != "" && Request["customerName"].ToString() != "-1")
            {
                sqlString += " AND TheName like '%" + Request["customerName"].ToString() + "%'";
            }

            sqlString += " AND DATEDIFF(month,GETDATE(),EndDate)<=1 AND CurrentState='0' AND verifyDate is not null";


            businesslist = businessbll.GetList(pageSize, pageIndex, sqlString, keyWord, order, out count);
            listdata.total = count;
            listdata.rows = businesslist;
            string json = serializer.Serialize(listdata);
            return Content(json);
        }

        public ActionResult GetBillUnit()
        {
            List<TheOffice> billUnit = new List<TheOffice>();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            TheOfficeBLL officebll = new TheOfficeBLL();
            billUnit = officebll.GetBillUnit();
            string json = serializer.Serialize(billUnit);
            return Content(json);
        }

        public ActionResult GetParentType()
        {
            List<BusinessType> typeList = new List<BusinessType>();
            BusinessTypeBLL typebll = new BusinessTypeBLL();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            typeList = typebll.GetParent();
            string json = serializer.Serialize(typeList);
            return Content(json);
        }

        public ActionResult GetChildTypeAddAll(string id)
        {
            List<BusinessType> typeList = new List<BusinessType>();
            BusinessTypeBLL typebll = new BusinessTypeBLL();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            int parentID = int.Parse(id);
            typeList = typebll.GetChildrenAddAll(parentID);
            string json = serializer.Serialize(typeList);
            return Content(json);
        }

        public ActionResult GetChildType(string id)
        {
            List<BusinessType> typeList = new List<BusinessType>();
            BusinessTypeBLL typebll = new BusinessTypeBLL();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            int parentID = int.Parse(id);
            typeList = typebll.GetChildren(parentID);
            string json = serializer.Serialize(typeList);
            return Content(json);
        }

        public ActionResult GetDepartments()
        {
            List<TheOffice> departments = new List<TheOffice>();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            TheOfficeBLL officebll = new TheOfficeBLL();
            departments = officebll.GetDepartments();
            string json = serializer.Serialize(departments);
            return Content(json);
        }

        public ActionResult GetOffice()
        {
            List<TheOffice> offices = new List<TheOffice>();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            TheOfficeBLL officebll = new TheOfficeBLL();
            offices = officebll.GetOffice();
            string json = serializer.Serialize(offices);
            return Content(json);
        }

        public ActionResult GetBusiness(int id)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            BusinessBLL businessbll = new BusinessBLL();
            Business businessinfo = new Business();
            businessinfo = businessbll.Get(id);
            string json = serializer.Serialize(businessinfo);
            return Content(json);
        }

        public ActionResult GetPayMethod()
        {
            List<PayMethod> method = new List<PayMethod>();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            PayMethodBLL methodbll = new PayMethodBLL();
            method = methodbll.GetList();
            string json = serializer.Serialize(method);
            return Content(json);
        }

        public ActionResult GetDepartmentName(int id)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            TheOfficeBLL officebll = new TheOfficeBLL();
            string name = officebll.GetDepartmentName(id);
            var result = new
            {
                DepartmentName = name
            };
            string json = serializer.Serialize(result);
            return Content(json);
        }

        public ActionResult GetBillUnitName(int id)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            TheOfficeBLL officebll = new TheOfficeBLL();
            string name = officebll.GetBillUnitName(id);
            var result = new
            {
                BillUnitName = name
            };
            string json = serializer.Serialize(result);
            return Content(json);
        }

        public ActionResult GetOfficeName(int id)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            TheOfficeBLL officebll = new TheOfficeBLL();
            string name = officebll.GetOfficeName(id);
            var result = new
            {
                OfficeName = name
            };
            string json = serializer.Serialize(result);
            return Content(json);
        }

        public ActionResult GetMethodName(int id)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            PayMethodBLL methodbll = new PayMethodBLL();
            string name = methodbll.GetName(id);
            var result = new
            {
                MethodName = name
            };
            string json = serializer.Serialize(result);
            return Content(json);
        }

        public ActionResult DealWithOverTime(string ids, string result)
        {
            String[] id = ids.Split(',');
            Message msg = new Message(true, "操作成功。");
            int i = 0;
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            BusinessBLL businessbll = new BusinessBLL();
            if (ids != null)
            {
                try
                {
                    i = businessbll.UpdateOverTime(id, result);
                }
                catch (Exception e)
                {
                    msg = new Message(false, e.Message);
                }
            }
            else
            {
                msg = new Message(false, "系统错误，请联系系统管理员：传值未成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult SaveData()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Message msg= new Message(false, "未保存成功。");
            Business businessinfo = new Business();
            BusinessBLL businessbll = new BusinessBLL();
            int i = 0;
            try
            {
                businessinfo.BusinessCode = Request.Form["BusinessCode"].ToString();
                businessinfo.CustomerID = int.Parse(Request.Form["CustomerID"].ToString());
                businessinfo.BusinessType1 = Request.Form["BusinessType1"].ToString();
                businessinfo.BusinessType2 = Request.Form["BusinessType2"].ToString();
                string begindate = Request.Form["BeginDate"].ToString();
                string enddate = Request.Form["EndDate"].ToString();
                businessinfo.Contract = Request.Form["Contract"].ToString();
                businessinfo.Charges = Convert.ToDecimal(Request.Form["Charges"].ToString());
                businessinfo.OwingMoney = businessinfo.Charges * -1;
                businessinfo.Introducer = Request.Form["Introducer"].ToString();
                businessinfo.Agent = Request.Form["Agent"].ToString();
                businessinfo.TheOffice = Request.Form["TheOffice"].ToString();
                businessinfo.TheDepartment = Request.Form["TheDepartment"].ToString();
                businessinfo.Items = Request.Form["Items"].ToString();
                businessinfo.FillIn = Session["TrueName"].ToString();
                businessinfo.FillInID = Convert.ToInt32(Session["UserID"].ToString());
                businessinfo.FillInDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
                businessinfo.Payment = Convert.ToInt32(Request.Form["Payment"].ToString());
                if (begindate != "" && enddate != "")
                {
                    businessinfo.BeginDate = Convert.ToDateTime(begindate);
                    businessinfo.EndDate = Convert.ToDateTime(enddate);
                }
                else
                {
                    businessinfo.BeginDate = null;
                    businessinfo.EndDate = null;
                }
                i = businessbll.Save(businessinfo);
            }
            catch (Exception e)
            {
                msg = new Message(false, e.Message);
            }
            if (i > 0)
            {
                msg = new Message(true, "保存成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult UpdateData()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Message msg = new Message(false, "未修改成功。");
            Business businessinfo = new Business();
            BusinessBLL businessbll = new BusinessBLL();
            int i = 0;
            try
            {
                businessinfo.ID = int.Parse(Request.Form["ID"].ToString());
                businessinfo.BusinessCode = Request.Form["BusinessCode"].ToString();
                businessinfo.BusinessType = Request.Form["BusinessType"].ToString();
                businessinfo.TypeName = Request.Form["TypeName"].ToString();
                string begindate = Request.Form["BeginDate"].ToString();
                string enddate = Request.Form["EndDate"].ToString();
                businessinfo.Contract = Request.Form["Contract"].ToString();
                businessinfo.Charges = Convert.ToDecimal(Request.Form["Charges"].ToString());
                businessinfo.Introducer = Request.Form["Introducer"].ToString();
                businessinfo.Agent = Request.Form["Agent"].ToString();
                businessinfo.TheOffice = Request.Form["TheOffice"].ToString();
                businessinfo.TheDepartment = Request.Form["TheDepartment"].ToString();
                businessinfo.Items = Request.Form["Items"].ToString();
                businessinfo.Payment = Convert.ToInt32(Request.Form["Payment"].ToString());
                if (begindate != "" && enddate != "")
                {
                    businessinfo.BeginDate = Convert.ToDateTime(begindate);
                    businessinfo.EndDate = Convert.ToDateTime(enddate);
                }
                else
                {
                    businessinfo.BeginDate = null;
                    businessinfo.EndDate = null;
                }
                if (Request.Form["updateBill"] != null && Request.Form["updateBill"].ToString() == "1")
                {
                    Charging billinfo = new Charging();
                    BusinessBLL billbll = new BusinessBLL();
                    billinfo.BusinessID = int.Parse(Request.Form["ID"].ToString());
                    if (Request.Form["ChargingMoney"] != null && Request.Form["ChargingMoney"].ToString() != "")
                    {
                        billinfo.ChargingMoney = Convert.ToDecimal(Request.Form["ChargingMoney"].ToString());
                    }
                    else
                    {
                        billinfo.ChargingMoney = null;
                    }
                    if (Request.Form["ChargingDate"] != null && Request.Form["ChargingDate"].ToString() != "")
                    {
                        billinfo.ChargingDate = Convert.ToDateTime(Request.Form["ChargingDate"].ToString());
                    }
                    else
                    {
                        billinfo.ChargingDate = null;
                    }
                    if (Request.Form["PayMethod"] != null && Request.Form["PayMethod"].ToString() != "")
                    {
                        billinfo.PayMethod = Request.Form["PayMethod"].ToString();
                    }
                    else
                    {
                        billinfo.PayMethod = null;
                    }
                    if (Request.Form["BillDate"] != null && Request.Form["BillDate"].ToString() != "")
                    {
                        billinfo.BillDate = Convert.ToDateTime(Request.Form["BillDate"].ToString());
                    }
                    else
                    {
                        billinfo.BillDate = null;
                    }

                    if (Request.Form["BillDepartment"] != null && Request.Form["BillDepartment"].ToString() != "" && Request.Form["BillDepartment"].ToString() != "-1")
                    {
                        billinfo.BillDepartment = int.Parse(Request.Form["BillDepartment"].ToString());
                    }
                    else
                    {
                        billinfo.BillDepartment = null;
                    }

                    if (Request.Form["BillMoney"] != null && Request.Form["BillMoney"].ToString() != "")
                    {
                        billinfo.BillMoney = Convert.ToDecimal(Request.Form["BillMoney"].ToString());
                    }
                    else
                    {
                        billinfo.BillMoney = null;
                    }
                    billbll.SaveBill(billinfo);
                }
                i = businessbll.Update(businessinfo);
            }
            catch (Exception e)
            {
                msg = new Message(false, e.Message);
            }
            if (i > 0)
            {
                msg = new Message(true, "保存成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult DeleteBusiness(string ids)
        {
            String[] id = ids.Split(',');
            Message msg = new Message(true, "操作成功。");
            int i = 0;
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            BusinessBLL businessbll = new BusinessBLL();
            if (ids != null)
            {
                try
                {
                    i = businessbll.Delte(id);
                }
                catch (Exception e)
                {
                    msg = new Message(false, e.Message);
                }
            }
            else
            {
                msg = new Message(false, "系统错误，请联系系统管理员：传值未成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult SaveCheck()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            BusinessBLL businessbll = new BusinessBLL();
            Charging charging = new Charging();
            decimal chargingMoney = Convert.ToDecimal(Request.Form["ChargingMoney"].ToString());
            string chargingDate = Request.Form["ChargingDate"].ToString();
            string payMethod = Request.Form["PayMethod"].ToString();
            string verifier = Session["UserName"].ToString();
            int id = int.Parse(Request.Form["ID"].ToString());
            Message msg = new Message(false, "操作失败。");
            int i = 0;

            charging.ID = id;
            charging.ChargingMoney = chargingMoney;
            charging.Verifier = verifier;
            charging.PayMethod = payMethod;
            charging.BusinessID = id;
            charging.VerifyDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            charging.InputDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());

            if (Request.Form["ChargingDate"] != null && Request.Form["ChargingDate"].ToString() != "")
            {
                charging.ChargingDate = Convert.ToDateTime(Request.Form["ChargingDate"].ToString());
            }
            else
            {
                charging.ChargingDate = null;
            }

            if (Request.Form["BillDate"] != null && Request.Form["BillDate"].ToString()!="")
            {
                charging.BillDate = Convert.ToDateTime(Request.Form["BillDate"].ToString());                
            }
            else
            {
                charging.BillDate = null;
            }

            if (Request.Form["BillDepartment"] != null && Request.Form["BillDepartment"].ToString() != "" && Request.Form["BillDepartment"].ToString()!="-1")
            {
                charging.BillDepartment = int.Parse(Request.Form["BillDepartment"].ToString());
            }
            else
            {
                charging.BillDepartment = null;
            }

            if (Request.Form["BillMoney"] != null && Request.Form["BillMoney"].ToString() != "")
            {
                charging.BillMoney = Convert.ToDecimal(Request.Form["BillMoney"].ToString());
            }
            else
            {
                charging.BillMoney = null;
            }

            try
            {
                i = businessbll.SaveCheck(charging);
            }
            catch (Exception e)
            {
                msg = new Message(false, e.Message);
            }
            if (i > 0)
            {
                msg = new Message(true, "操作成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult SaveCharging()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            BusinessBLL businessbll = new BusinessBLL();
            ChargingData charging = new ChargingData();
            int id = int.Parse(Request.Form["ID"].ToString());
            Message msg = new Message(false, "操作失败。");
            int i = 0;
            if (Request.Form["Paidup"] == null)
            {
                decimal chargingMoney = Convert.ToDecimal(Request.Form["ChargingMoney"].ToString());
                string chargingDate = Request.Form["ChargingDate"].ToString();
                string payMethod = Request.Form["PayMethod"].ToString();
                charging.ChargingMoney = chargingMoney;
                charging.PayMethod = payMethod;
                charging.BusinessID = id;
                charging.InputDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
                if (chargingDate == "" || chargingDate == null)
                {
                    charging.ChargingDate = null;
                }
                else
                {
                    charging.ChargingDate = Convert.ToDateTime(chargingDate);
                }

                try
                {
                    i = businessbll.SaveCharging(charging);
                }
                catch (Exception e)
                {
                    msg = new Message(false, e.Message);
                }
            }
            else
            {
                try
                {
                    i = businessbll.UpdatePaidup(id);
                }
                catch (Exception e)
                {
                    msg = new Message(false, e.Message);
                }
            }
            if (i > 0)
            {
                msg = new Message(true, "操作成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult GetChargingList(int businessid)
        {
            BusinessBLL businessbll = new BusinessBLL();
            ChargingLog charginglog = new ChargingLog();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            charginglog = businessbll.GetChargingLog(businessid);
            string json = serializer.Serialize(charginglog);
            return Content(json);
        }

        public ActionResult GetPayment()
        {
            BusinessBLL businessbll = new BusinessBLL();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<PaymentType> paymentlist = new List<PaymentType>();
            paymentlist = businessbll.GetPayment();
            string json = serializer.Serialize(paymentlist);
            return Content(json);
        }

        public ActionResult CountBillByUnit()
        {
            BusinessBLL businessbll = new BusinessBLL();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<CountBill> countbill = new List<CountBill>();
            string date1 = "", date2 = "";
            if (Request.Form["Date1"] != null && Request.Form["Date1"].ToString() != "")
            {
                date1 = Request.Form["Date1"].ToString();
            }
            if (Request.Form["Date2"] != null && Request.Form["Date2"].ToString() != "")
            {
                date2 = Request.Form["Date2"].ToString();
            }
            if (date1 == "" || date2 == "")
            {
                date1 = DateTime.Now.Year.ToString() + "-1-1";
                date2 = DateTime.Now.ToShortDateString();
            }
            countbill = businessbll.CountBill(date1, date2);
            string json = serializer.Serialize(countbill);
            return Content(json);
        }

        public ActionResult CountBillByOffice()
        {
            BusinessBLL businessbll = new BusinessBLL();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<CountBillbyOffice> countbill = new List<CountBillbyOffice>();
            string date1 = "", date2 = "";
            if (Request.Form["Date1"] != null && Request.Form["Date1"].ToString() != "")
            {
                date1 = Request.Form["Date1"].ToString();
            }
            if (Request.Form["Date2"] != null && Request.Form["Date2"].ToString() != "")
            {
                date2 = Request.Form["Date2"].ToString();
            }
            if (date1 == "" || date2 == "")
            {
                date1 = DateTime.Now.Year.ToString() + "-1-1";
                date2 = DateTime.Now.ToShortDateString();
            } 
            countbill = businessbll.CountBillByOffice(date1, date2);
            string json = serializer.Serialize(countbill);
            return Content(json);
        }

        public ActionResult GetOfficeCustomer()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<Customer> customerlist = new List<Customer>();
            CustomerBLL customerbll = new CustomerBLL();
            CustomerData customerdata = new CustomerData();
            string order = Request.Form["order"].ToString();
            int pageIndex = int.Parse(Request.Form["page"].ToString());
            int pageSize = int.Parse(Request.Form["rows"].ToString());
            string keyWord = Request.Form["sort"].ToString();
            int count = 0;
            string customer = "";
            if (Request.Form["customer"] != null && Request.Form["customer"].ToString() != "")
            {
                customer = Request.Form["customer"].ToString();
            }
            try
            {
                int authid = int.Parse(Session["AuthID"].ToString());
                int keyid = int.Parse(Session["OfficeID"].ToString());
                if (authid == 1)
                {
                    keyid = int.Parse(Session["UserID"].ToString());
                }
                
                string where = customerbll.MakeWhereString(authid,keyid,customer);             
                customerlist = customerbll.GetLlist(pageSize, pageIndex, where, keyWord, order, out count);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            customerdata.total = count;
            customerdata.rows = customerlist;
            string json = serializer.Serialize(customerdata);
            return Content(json);
        }

        public ActionResult GetCustomerBusiness()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string order = Request.Form["order"].ToString();
            //int pageIndex = int.Parse(Request.Form["page"].ToString());
            //int pageSize = int.Parse(Request.Form["rows"].ToString());
            string keyWord = Request.Form["sort"].ToString();
            string whereString = "1=1";
            int count;
            BusinessBLL businessbll = new BusinessBLL();
            List<Business> businesslist = new List<Business>();
            BusinessData listdata = new BusinessData();
            int pageSize = 0, pageIndex = 0;
            if (Request.Form["customerid"] != null)
            {
                int customerid = int.Parse(Request.Form["customerid"].ToString());
                whereString += " And CustomerID=" + customerid;
            }
            businesslist = businessbll.GetList(pageSize, pageIndex, whereString, keyWord, order, out count);
            listdata.total = count;
            listdata.rows = businesslist;
            string json = serializer.Serialize(listdata);
            return Content(json);
        }

        public ActionResult SaveReBill()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            BusinessBLL businessbll = new BusinessBLL();
            Charging charging = new Charging();
            int id = int.Parse(Request.Form["ID"].ToString());
            Message msg = new Message(false, "操作失败。");
            int i = 0;

            decimal billMoney = Convert.ToDecimal(Request.Form["BillMoney"].ToString());
            string billDate = Request.Form["BillDate"].ToString();
            string billUnit = Request.Form["BillDepartment"].ToString();
            charging.BusinessID = id;
            charging.BillDate = Convert.ToDateTime(billDate);
            charging.BillDepartment = int.Parse(billUnit);
            charging.BillMoney = billMoney;
            try
            {
                i = businessbll.SaveBill(charging);
            }
            catch (Exception e)
            {
                msg = new Message(false, e.Message);
            }


            if (i > 0)
            {
                msg = new Message(true, "操作成功。");
            }
            string json = serializer.Serialize(msg);
            return Content(json);
        }

        public ActionResult GetCount()
        {
            BusinessBLL businessbll = new BusinessBLL();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<CountResult> countlist = new List<CountResult>();
            string date1 = "", date2 = "";
            string ctype = "";
            if (Request.Form["Date1"] != null && Request.Form["Date1"].ToString() != "")
            {
                date1 = Request.Form["Date1"].ToString();
            }
            if (Request.Form["Date2"] != null && Request.Form["Date2"].ToString() != "")
            {
                date2 = Request.Form["Date2"].ToString();
            }
            if (Request.Form["ctype"] != null && Request.Form["ctype"].ToString() != "")
            {
                ctype = Request.Form["ctype"].ToString();
            }
            if (date1 == "" || date2 == "")
            {
                date1 = DateTime.Now.Year.ToString() + "-1-1";
                date2 = DateTime.Now.ToShortDateString();
            }
            countlist = businessbll.CountBusiness(ctype, date1, date2);
            string json = serializer.Serialize(countlist);
            return Content(json);
        }

        public ActionResult CountExportToExcel()
        {
            DataTable table = new DataTable();
            BusinessBLL businessbll = new BusinessBLL();
            BusinessTypeBLL typebll = new BusinessTypeBLL();
            List<Business> businesslist = new List<Business>();
            string json = Request.Params["data"];
            table = DataRenderToExcel.JsonToDataTable(json);
            MemoryStream output = DataRenderToExcel.RenderCountToExcel(table) as MemoryStream;
            //Return the result to the end user
            return File(output.ToArray(),   //The binary data of the XLS file
             "application/vnd.ms-excel",//MIME type of Excel files
             "汇总表" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls");
        }

        public ActionResult LogOff()
        {
            Session["UserName"] = null;
            Session["TrueName"] = null;
            Session["UserID"] = null;
            Session["OfficeID"] = null;
            Session["AuthID"] = null;
            return RedirectToAction("Index","Home");
        }
    }
}
